<?php
/**
 * Helper class for creating html elments
 *
 */
class Html_Form
{
    /**
     * Checks is given element is selected
     *
     * @param mixed $selected selected elements
     * @param mixed $k tested element
     * @return bool result
     */
    function isSelected($selected, $k)
    {
        if (! is_array($selected))
            $selected = array($selected);
        return (in_array($k, $selected) ? 'selected' : '');
    }

    /**
     * Creates html options
     *
     * @param array $vals hash of key=>value
     * @param mixed $selected selected element(s)
     * @param string $all name of first empty option, usually 'All'
     * @return string
     */
    function makeOptions($vals, $selected, $all='')
    {
        $html = '';
        if ($all)
            $html .= '<option value="">' . $all . '</option>';
        foreach ($vals as $k => $label)
            $html .= '<option ' . Html_Form::isSelected($selected, $k) . ' value="' . $k . '">' . $label . '</option>';
        return $html;
    }

    /**
     * Checks if given element is checked
     *
     * @param mixed $selected
     * @param string $k
     * @return string
     */
    function isChecked($selected, $k)
    {
        if (! is_array($selected))
            $selected = array($selected);
        return (in_array($k, $selected) ? 'checked' : '');
    }

    /**
     * Creates html radios
     *
     * @param string $name
     * @param array $vals
     * @param string $selected
     * @return string
     */
    function makeRadios($name, $vals, $selected)
    {
        $html = '';
        foreach ($vals as $k => $label)
            $html .= '<input type="radio" name="'.$name.'" value="' . $k . '" ' . Html_Form::isChecked($selected, $k) . '>'.$label;
        return $html;
    }

    /**
     * Enter description here...
     *
     * @param string $name prefix of input name
     * @param string $phone phone value as string delimited with '-'
     * @return string html code
     */
    function makePhone($name, $phone, $className = "header-link")
    {
        $v1 = $v2 = $v3 = '';
        $a = explode('-', $phone);
        if (3 == sizeof($a))
            list($v1, $v2, $v3) = $a;

        $input = '<td><input class="txt" name="' . $name . '_1" type="text" size="10" value="' . $v1 . '"  onchange="_phone(\''.$name. '\')" />&nbsp;&nbsp;</td>'
               . '<td><input class="txt" name="' . $name . '_2" type="text" size="17" value="' . $v2 . '"  onchange="_phone(\''.$name. '\')" />&nbsp;&nbsp;</td>'
               . '<td><input class="txt" name="' . $name . '_3" type="text" size="17" value="' . $v3 . '"  onchange="_phone(\''.$name. '\')" />'
               . '<input type="hidden" name="'. $name .'" value="'. $phone .'"></td>';

        $comment = '(ex. 63 the country code 917 the area code and 1234567 the number)';
        if (in_array($name, array('mobile','phone','phone2','fax')))
        {
            $input = '<td colspan=3><input type="text" name="'. $name .'" value="'. $phone .'" class="txt"></td>';
            $comment = 'Example (+63255555555) ';
        }

        $html  = '<table border="0" cellpadding ="0" cellspacing="2">'
               . '<tr>'
               . $input
               . '</tr>'
               . '<tr>'
               . '  <td colspan="3"><span class="style29">' . $comment . '</span></td>'
               . '</tr>'
               . '</table>';

        return $html;
    }


    /**
     * Enter description here...
     *
     * @param string $name prefix of select and input
     * @param array $vals select values
     * @param string $selected selected value
     * @param string $other other value if select value=-1
     * @return html with select and input
     */
    function makeSelectOther($name, $vals, $selected = '', $other = '')
    {
        $style = ($selected == -1 ? 'block': 'none');

        $html = '<table><tr><td><select name="data[' . $name . '_id]" onchange="htmlFormOther(this, \'' . $name . '\')">';
        $vals = $vals + array(-1 => 'Other');
        $html .= Html_Form::makeOptions($vals, $selected, 'Please Select');
        $html .= '</select></td><td>';
        $html .= '<input type="text" class="txt" id="' . $name . '" value="' . $other . '" name="data['. $name . '_other]"'
               . ' style="display:'. $style .'"></td></tr></table>';
        return $html;
    }
}

?>
